<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>管理员筛选</title>
<link rel="stylesheet" type="text/css" href="/css/select.css" />
<script src="/jquery/jquery.min.js"></script>
<script src="/utils/TimeUtil.js"></script>
<script src="/utils/HttpUtil.js"></script>
<script>
	var global = {
		page : 1,
		size : 10,
		totalPage : 0,
		total : 0
	};
	var params = {};

	$(document).ready(function() {
		importAdmin();
		query();
	});

	function setParams() {
		params.page = global.page;
		params.size = $("#size").val();
		params.username = $("#username").val();
		params.realname = $("#realname").val();
	}

	function suss(data) {
		init();
		if (data.code == 0) {
			result = data.data;
			putSheet(result.list);
			setPage(result);
		} else {
			$("#msg").text(data.msg + "[" + data.code + "]");
		}
	}

	function err(data) {
		$("#msg").text("网络错误(" + data.status + ")")
	}

	function query() {
		setParams();
		post("/admin/select", params, suss, err);
	}

	function init() {
		$("#sheet").text("");//清空
		$("#msg").text("");
	}

	function putSheet(list) {
		var sheet = $("#sheet");
		if (list.length <= 0) {
			$("#msg").text("无数据");
			return;
		}
		for (index in list) {
			var x = list[index];
			li = "<tr>" + "<td>" + x.id + "</td>" + "<td>" + x.username
					+ "</td>" + "<td>" + x.realname + "</td>" + "<td>"
					+ x.phone + "</td>" + "<td>" + x.email + "</td>"
					+ "<td><strong onclick='updateUseStatus(" + x.id + ")'>"
					+ status(x.useStatus) + "</strong></td>" + "<td>"
					+ timestampToTime(x.regTime) + "</td>" + "<td>"
					+ "<strong onclick='toSave(" + x.id + ")'>编辑</strong>"
					+ "</td>" + "</tr>";
			sheet.append(li);
		}
	}

	function status(status) {
		if (status == 0) {
			return "不可用";
		}
		return "可用";
	}

	function setPage(result) {
		global.page = result.page;
		global.size = result.size;
		global.totalPage = result.totalPage;
		global.total = result.total;
		setPageText();
	}

	function next() {
		if (global.page < global.totalPage) {
			global.page++;
			query();
		}
	}

	function pre() {
		if (global.page > 1) {
			global.page--;
			query();
		}
	}

	function toPage() {
		global.page = $("#toPage").val();
		query();
	}

	function setPageText() {
		$("#curPage").text(global.page);
		$("#totalPage").text(global.totalPage);
		$("#total").text(global.total);
		$("#toPage").val(global.page);
		$("#size").val(global.size);
	}

	document.onkeydown = function(event) {
		var e = event || window.event || arguments.callee.caller.arguments[0];
		if (e && e.keyCode == 13) {
			$("#query").click();
		}
	}

	function toSave(id) {
		window.location.href = 'save.html?id=' + id;
	}

	function updateUseStatus(id) {
		post("/admin/updateUseStatus", {
			id : id
		}, function(data) {
			if (data.code == 0) {
				query();
			} else {
				$("#msg").text(data.msg + "[" + data.code + "]");
			}
		});
	}

	function importAdmin() {
		$.ajax({
			url : "/template/adminInfo.html", //这里是静态页的地址
			type : "GET", //静态页用get方法，否则服务器会抛出405错误
			dataType : "html",
			success : function(data) {
				$("#admin").html(data);
			}
		});
	}
</script>
</head>
<body>
	<div id="admin"></div>
	<h1>
		<a href="/stockHome.html">首页</a>
	</h1>
	<h1>第三方系统</h1>
	<h3>
		<a href="save.html">添加</a>
	</h3>
	<h4 id="msg"></h4>
	<div id="select">
		<table>
			<tr>
				<td>登录名:</td>
				<td><input id="username" type="text"></input></td>
				<td>昵称:</td>
				<td><input id="realname" type="text"></input></td>
				<td><input id="query" type="button" onclick="query()"
					value="查询"></input></td>
			</tr>
		</table>
	</div>
	<div id="show">
		<table border="1">
			<thead>
				<tr>
					<th>ID</th>
					<th>登录名</th>
					<th>昵称</th>
					<th>手机</th>
					<th>邮箱</th>
					<th>可用状态</th>
					<th>注册时间</th>
					<th>操作</th>
				</tr>
			</thead>
			<tbody id="sheet">
			</tbody>
		</table>
	</div>
	<div id="page">
		<p>
			<strong onclick="pre()">上一页</strong> <strong>当前[<i
				id="curPage">1</i>]页
			</strong> <strong onclick="next()">下一页</strong> <strong>共[<i
				id="totalPage">1</i>]页
			</strong> <strong><i onclick="toPage()">跳转</i><input id="toPage"
				type="number" min="1" style="width: 30px;"></input>页</strong> <strong>每页<input
				id="size" type="number" min="1" value="10" style="width: 30px;"></input>条
			</strong> <strong>共[<i id="total">1</i>]条数据
			</strong>
		</p>
	</div>
</body>
</html>